package com.lxg.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author 陆小根
 * date: 2022/04/26
 * Description:
 */

@Component
@Aspect
public class MyAdvice {
  @Pointcut("execution(void com.lxg.dao.BookDao.update())")
  private void pt(){}

  @Pointcut("execution(int com.lxg.dao.BookDao.select())")
  private void pt2(){}

  @Before("pt()")
  // 此处也可以写成 @Before("MyAdvice.pt()") 不建议
  public void before() {
    System.out.println("before advice ...");
  }


//  @After("pt()")
  public void after() {
    System.out.println("after advice ...");
  }

//  @Around("pt()")
//  public void around(ProceedingJoinPoint pjp) throws Throwable {
//    System.out.println("around before advice ...");
//    pjp.proceed();
//    System.out.println("around after advice ...");
//  }

//  @Around("pt2()")
  public Object around(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("around before advice ...");
    Integer ret = (Integer) pjp.proceed(); // 表示对原始操作的调用
    System.out.println("around after advice ...");
    return ret;
  }

//  @AfterReturning("pt2()")
  public void afterReturning() {
    System.out.println("afterReturning advice ...");
  }

  @AfterThrowing("pt2()")
  public void afterThrowing() {
    System.out.println("afterThrowing advice ...");
  }
}
